package com.reggie.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.ResultResponseVo;
import com.reggie.common.enums.backend.CategoryEnum;
import com.reggie.common.enums.backend.EmployeeEnum;
import com.reggie.module.entity.dto.categoryDto.AddCategoryDto;
import com.reggie.module.entity.dto.categoryDto.UpdateCategoryDto;
import com.reggie.module.entity.po.Category;
import com.reggie.module.entity.po.Employee;
import com.reggie.module.mapper.CategoryMapper;
import com.reggie.module.mapper.DishMapper;
import com.reggie.module.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 早睡蛋
 * @Date 2022/9/17 14:29:22
 * @Desc:
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;


    /**
     * 菜单分类分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public ResultResponseVo selectByPage(int page, int pageSize) {
        Page<Category> categoryPage = new Page<>(page, pageSize, true);
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.select("id", "name", "type", "sort", "update_time").last("order by sort");
        IPage<Category> categoryIPage = categoryMapper.selectPage(categoryPage, categoryQueryWrapper);
        return CategoryEnum.SUCCESS.getResult().setData(categoryIPage);
    }

    /**
     * 删除菜单
     *
     * @param ids
     * @return
     */
    @Override
    public ResultResponseVo deleteById(Long ids) {
        Integer isExist = dishMapper.dishIsExist(ids);
        System.out.println(isExist);
        if (isExist != null) {
            return CategoryEnum.DISH_EXIST.getResultResponseVo();
        }
        int i = categoryMapper.deleteById(ids);
        if (i > 0) {
            return CategoryEnum.DEL_OK.getResultResponseVo();
        }
        return CategoryEnum.DEL_FAIL.getResultResponseVo();
    }

    /**
     * 新增菜单分类
     *
     * @param addCategoryDto
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResultResponseVo add(AddCategoryDto addCategoryDto, HttpServletRequest request) {
        String name = addCategoryDto.getName();
        Category checkName = categoryMapper.checkName(name);
        if (checkName != null) {
            return CategoryEnum.CATEGORY_EXIST.getResultResponseVo();
        }
        Category add = addCategoryDto.add(request);
        boolean save = this.save(add);
        if (save) {
            return CategoryEnum.ADD_OK.getResultResponseVo();
        }
        return CategoryEnum.ADD_FAIL.getResultResponseVo();
    }

    /**
     * 修改菜品分类
     * @param updateCategoryDto
     * @param request
     * @return
     */
    @Override
    public ResultResponseVo updateCategory(UpdateCategoryDto updateCategoryDto, HttpServletRequest request) {
        String name = updateCategoryDto.getName();
        Category category = categoryMapper.checkName(name);
        if (category != null && !category.getId().equals(updateCategoryDto.getId())) {
            return CategoryEnum.CATEGORY_EXIST.getResultResponseVo();
        }
        UpdateCategoryDto result = new UpdateCategoryDto(updateCategoryDto.getId(), updateCategoryDto.getName(), updateCategoryDto.getSort(), request);
        Integer flag = categoryMapper.updateCategory(result);
        if (flag > 0) {
            return CategoryEnum.UPDATE_OK.getResultResponseVo();
        }
        return CategoryEnum.UPDATE_FAIL.getResultResponseVo();
    }

    /**
     * 获取菜品分类
     * @param category
     * @return
     */
    @Override
    public ResultResponseVo listByType(Category category) {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.select("id", "name", "sort","type")
                .last("order by sort");
        if (category.getType()!=null) {
            categoryQueryWrapper.eq("type",category.getType());
        }
        List<Category> categoryList = categoryMapper.selectList(categoryQueryWrapper);
        return CategoryEnum.SUCCESS.getResultResponseVo().setData(categoryList);
    }
}
